<?php

/**
 * Created by PhpStorm.
 * User: WhiteBlue
 * Date: 15/3/2
 * Time: 下午4:45
 */
class Controller
{
    private $params;
    private $method;

    protected $view;

    public function __destruct()

    {

        $view = $this->view;

        if ($view instanceof view) {
            if (!empty($view->data)) {
                extract($view->data);
            }

            require $view->view;

        }

    }

    public function index()
    {
        echo '未找到主方法' . '<br>';
    }

    public function setParams($params)
    {
        $this->params = $params;
    }

    public function setMethod($method)
    {
        $this->method = $method;
    }

    public function getMethod()
    {
        return $this->method;
    }

    public function getParam($num, $default)
    {
        if (isset($this->params[$num])) {
            return htmlspecialchars($this->params[$num]);
        } else {
            return $default;
        }
    }

    public function redirect($url)
    {
        header('location:http://' . URL_BASE . '?' . $url);
    }

    public function render404()
    {
        $this->view = view::make('error')->with('error', '404!页面未找到');
    }

    public function renderError($error)
    {
        $this->view = view::make('error')->with('error', $error);
    }

}